﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.IO.Ports;
using System.Runtime.Serialization.Formatters.Binary;

namespace AssemblyCYG
{
    public static class ProjectInfo
    {
        public static ProjectPara Parameter;

        public static void SaveParameter(string configPath)
        {
            FormatFile(configPath, Parameter);
        }
        public static void ReadProjectConfig(string configPath)
        {
            Parameter = new ProjectPara();
            ReadParameter(configPath);
        }
        public static void ReadParameter(string configPath)
        {
            if (File.Exists(configPath))
            {
                Parameter = (ProjectPara)DeformatFile(configPath);
            }
            else
            {
                Parameter = ProjectPara.GetDefaultDeviceParameter();
            }
        }
        public static void FormatFile(string FileName, object obj)
        {
            if (!(FileName != ""))
                return;
            Stream serializationStream = (Stream)File.Open(FileName, FileMode.Create);
            new BinaryFormatter().Serialize(serializationStream, obj);
            serializationStream.Close();
        }
        public static object DeformatFile(string FileName)
        {
            if (!File.Exists(FileName))
                return (object)null;
            if (!(FileName != ""))
                return (object)null;
            Stream serializationStream = (Stream)File.Open(FileName, FileMode.Open);
            object obj = new BinaryFormatter().Deserialize(serializationStream);
            serializationStream.Close();
            return obj;
        }
    }
}
